/**
 *
 * @author yuan_rz
 * @date 2019/02/21 17:50
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        layui = win.layui,
        table,
        vueData = {

        },
        table_where = {

        },
        cols = [[
              {type: 'checkbox', title: '选择', fixed: 'left'}
            , {field: 'ROW_ID', width: 60, title: '序号', fixed: 'left',hide : true}
            , {field: 'LOAD_NAME', width: 100, title: '任务名称'}
            , {field: 'LOAD_FILE', width: 100, title: '任务文件'}
            , {field: 'LOAD_PARAMS', width: 200, title: '额外参数'}
            , {field: 'LOAD_TRANDATE', width: 100, title: '上次同步日期'}
            , {
                width: 60,
                title: '',
                toolbar: '#script-loadsetting',
                fixed: 'right',
                align: 'center',
                unresize: true
            }
        ]],
        loadSettingAction,
        moduleName = '同步任务',
        editArea = ['360px', '330px']
    ;

    var load = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'load',
                _searchs: [{
                    id: 'load', tableId: 'load_table', datas: cols[0]
                }],
                _loadTplsFull: [{
                    dom: 'load_edit', tpl: 'system/load_edit'
                }]
            }, win);
        },
        initTable: function () {
            layui.use(['table'], function () {
                table = layui.table;
                table.render({
                    elem: app.getByid(doc, 'load_table'),
                    url: g_ctxPath + 'sys/load/getListData',
                    exportsUrl: g_ctxPath + 'sys/load/exportListData',
                    exportsName: moduleName + '数据',
                    title: moduleName + '表',
                    id: 'load_table',
                    toolbar: '#table-toolbar',
                    defaultToolbar: ['filter'],//, 'exports'
                    height: '300px',
                    cols: cols,
                    page: true,
                    where: table_where
                });

                layui.laytpl(app.getByid(doc, 'script-loadsetting-action').innerHTML).render({}, function (string) {
                    loadSettingAction = string;
                });

                var tipsIndex = null;

                table.on('tool(load_table)', function (obj) {
                    switch(obj.event) {
                        case 'detail':
                            layer.tips(loadSettingAction, obj.td, {
                                tips: [3, '']
                                , time: 5000
                                , anim: 500
                                , maxWidth: 120
                                , skin: 'layui-table-tips'
                                , success: function (layero, index) {
                                    tipsIndex = index;
                                    layero.find('li').on('click', function () {
                                        switch ($(this).attr('inx')) {
                                            case '1':
                                                self._openTplFull({
                                                    tag: 'load_edit',
                                                    moduleName: moduleName,
                                                    tagData: {
                                                        obj : obj
                                                    },
                                                    index: 0,
                                                    openOpts: {
                                                        title: moduleName + '编辑',
                                                        area: editArea
                                                    }
                                                });
                                                break;
                                            case '2':
                                                self.deleteData(obj);
                                                break;
                                        }
                                        layer.close(index);
                                        tipsIndex = null;
                                    });
                                }
                            });
                            return false;
                            break;
                    }
                });

                //监听行双击事件
                table.on('rowDouble(load_table)', function (obj) {
                    self._openTplFull({
                        tag: 'load_edit',
                        moduleName: moduleName,
                        tagData: {
                            obj : obj
                        },
                        index: 0,
                        openOpts: {
                            title: moduleName + '编辑',
                            area: editArea
                        }
                    });
                });

                //监听事件
                table.on('toolbar(load_table)', function (obj) {
                    switch (obj.event) {
                        case 'add':
                            self._openTplFull({
                                tag: 'load_edit',
                                moduleName : moduleName,
                                tagData: {
                                    is_add : true
                                },
                                index: 0,
                                openOpts: {
                                    title: moduleName + '编辑',
                                    area: editArea
                                }
                            });
                            break;
                        case 'del':
                            self.deleteDataBatch();
                            break;
                    }
                });
            });
        },
        reloadTable: function () {
            self._reloadTable({id: 'load_table', where: table_where});
        },
        deleteData: function (obj) {
            app.ajax({
                xurl: 'sys/load/deleteData',
                xdata: obj.data,
                confirm: true,
                loading: true,
                okmsg: moduleName +'信息删除成功',
                cb: function (result) {
                    if (result.ok) {
                        self.reloadTable();
                    }
                }
            });
        },
        deleteDataBatch: function(){
            var checkStatus = table.checkStatus('load_table');
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    var _temp = app.fields_('sys_load');
                    _temp.ID = v.ID;
                    _xdata.push(_temp);
                });
                app.ajax({
                    xurl: 'sys/load/deleteDataBatch',
                    xdata: _xdata,
                    confirm: true,
                    loading: true,
                    toJsonStr: true,
                    okmsg: moduleName +'信息删除成功',
                    cb: function (result) {
                        if (result.ok) {
                            self.reloadTable();
                        }
                    }
                });
            }
        },
        _ready: function () {
            vueData.serchData = table_where;
            this._super({
                vue: [{
                    el: '#page',
                    id: 'page',
                    data: vueData,
                    searchId: 'load',
                    methods: {

                    }
                }]
            });

            self.initTable();
        },
        _lazy: function () {
            this._super();
        }
    });

    return new load();

});